﻿clear all
set more off
set maxvar 20000

*Define directory with input data
cd "Q:"

*Create dataset with selected variables

*Create extract from 2001-2019 V1 RAND HRS CAMS data 
use "randcams_2001_2019v1.dta", clear

*Keep relevant variables - Aguiar and Hurst: total food expenditures, total nondurable expenditures, and total expenditures
local constant = "hhidpn*"
local camsvars = "incamsc* h*cndurf h*ctotsf h*cndur h*ctots h*cwgthh"
keep `constant' `camsvars' 

*Define number of waves
local maxwave=14
   
*Wave-specifc variables
local vars = "cndurf ctotsf cndur ctots cwgthh"

forvalues i=5/`maxwave' {
foreach v in `vars' {
	capture gen `v'`i'=h`i'`v' if incamsc`i'==1
}
}

*Reshape data
keep hhidpn cndurf5-ctots14
quietly des *11, varlist
local list=subinstr("`r(varlist)'","11","",.)
disp "`list'"
reshape long "`list'", i(hhidpn) j(wave) 
   
gen waveyr=1992+(wave-1)*2

drop if missing(cndurf, ctotsf, cndur, ctots, cwgthh)

tostring hhidpn, generate(hhidpn_string)
gen count=strlen(hhidpn_string)
replace hhidpn_string="0"+hhidpn_string if count==8
gen hhid=substr(hhidpn_string,1,6)
gen pn=substr(hhidpn_string,7,9)

drop wave count
save hrsrandcamsdata_2000_2018, replace

sort hhid waveyr
quietly by hhid waveyr:  gen dup = cond(_N==1,0,_n)
